package com.handu.luna.core.domain.order;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import com.handu.luna.core.domain.BaseRepository;

public interface OrderInfoRepository extends JpaRepository<OrderInfo, Long>,BaseRepository<OrderInfo>,JpaSpecificationExecutor<OrderInfo>{
	// order by createdDate desc  下面两个的查询先安排id升序来查询 为了测试方便 将可测试数据放于第一页

	@Query("select o from OrderInfo o  where deleted = false and isParent = true")
	Page<OrderInfo> findAllParent(Pageable pageable);
	
	@Query("select o from OrderInfo o where deleted = false and isParent = true")
	List<OrderInfo> findAllParent();
	
	/**
	 * 根據條件進行查找
	 */
	List<OrderInfo> findAll(Specification<OrderInfo> specification);
	
	
	/**
	 * 查找全部 包括删除掉的
	 * 根据父订单单号查找其所有的子订单 可能多个
	 * @param orderNumber
	 * @return
	 */
	@Query("select o from OrderInfo o where o.parentNumber = :parentNumber")
	List<OrderInfo> findAllChildsByOrderNumber(@Param("parentNumber")String parentNumber);
	
	
	/**
	 *查找未删除的 子订单
	 * @param orderNumber
	 * @return
	 */
	@Query("select o from OrderInfo o where o.parentNumber = :parentNumber and deleted = false ")
	List<OrderInfo> findNotDeletedChildsByOrderNumber(@Param("parentNumber")String parentNumber);
	
	/**
	 * 根据订单单号查找订单
	 * @param orderNumber
	 * @return
	 */
	@Query("select o from OrderInfo o where o.orderNumber = ?1")
	List<OrderInfo> findByOrderNumber(String orderNumber);
	

}
